#include <Wire.h>

//this test works when testing with boards 1 and 2
//set the threshold as a 1 time value, max is 3.3V (voltage reference removed from protos)
//crashes due to 50Hz noise
//input thresholds not checked 100%
//minimum input signal duration 40ns for coincidence.
//at 40ns incident coincidence the output lasts for 150ns - plenty for the DUE to capture.
//Hardware is more reliable than DUE's noise immunity.
//Note the MAX5387LAUD+ doesn't support I2C readback.

//detailed threshold calibration could be done, but our signal generator doesn't plug in very well.
//using DAC would be a nice option for checking the thresholds.

void setup() {
  // put your setup code here, to run once:
  pinMode(48, OUTPUT); //used to inject 3.3V signals if required with jumpers
  pinMode(49, OUTPUT); //used to inject as well
  pinMode(35, OUTPUT); //setup for the MAX5387 - later set to 0. signal PA0
  pinMode(36, OUTPUT);//setup for the MAX5387 - later set to 0. signal PA1
  pinMode(37, OUTPUT);//setup for the MAX5387 - later set to 0. signal PA2
  pinMode(A1, INPUT); //make sure the input pins are in high impedance mode
  pinMode(A2, INPUT);//make sure the input pins are in high impedance mode
  digitalWrite(35, LOW);//configure the address of the MAX5387 pot
  digitalWrite(36, LOW);//configure the address of the MAX5387 pot
  digitalWrite(37, LOW);//configure the address of the MAX5387 pot
  //pinMode(38, INPUT); //optional high impedance mode for strigA - note forcing signal high doesn't really work.
  //pinMode(39, INPUT);//optional high impedance mode for strigB - note forcing signal high doesn't really work.
  pinMode(5, INPUT); //set the interrupt pin for trigger as high impedance, probably not required
  attachInterrupt(38, striga, RISING); //interrupt on STRIGA
  attachInterrupt(39, strigb, RISING);  //interrupt on STRIGB
  attachInterrupt(5, realint, RISING); //interrupt on the real trigger - note rising, 
  
  //setup the serial for debug
  Serial.begin(9600);
  //setup the MAX5387 pot
  Wire.begin();
  Serial.print("MAX5387 Address ");
  Serial.print(0x28, HEX);
  Wire.beginTransmission(byte(0x28)); // transmit to device #112
  Wire.write(byte(B00010011));
  Wire.write(byte(0x50));// sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting
  Serial.println("threshold set to 0x50"); //note that this is manually updated by the human!
}

//interrupt routine for event on STRIGA signal (channel A high)
void striga(){
Serial.print("a");
}

//interrupt routine for event on STRIGB signal (channel B high)
void strigb(){
Serial.println("b");
}

//interrupt routine for event on TRIG signal (AND output high)
void realint(){
Serial.println("real");
}


void loop() {
  // put your main code here, to run repeatedly:
//nothing to do here at the moment.
//delay(100);

//digitalWrite(48, HIGH); //3.3v signal injections via pins 48 and 49 if required.
//digitalWrite(49, HIGH);


//digitalWrite(48, LOW);
//digitalWrite(49, LOW);
//delay(1000);
}